﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using Panny.Data;
using System.Data.SqlClient;
using DBUtility;//请先添加引用

namespace Easydp.Models
{
    public class AgentManager
    {

        public Entities.TT_Agent GetByLoginName(string loginame)
        {
            /// <summary>
            /// 得到一个对象实体
            /// </summary>
            Entities.TT_Agent obj = new Easydp.Models.Entities.TT_Agent();
            using (SqlText selection = new SqlText("SELECT * FROM TT_Agent where LoginName=@LoginName"))
            {
                selection.AddParameter("@LoginName", loginame);
                while (selection.Read())
                {
                    // Extract value of DataReader
                     obj.AgentID = Convert.ToInt32(selection["AgentID"].ToString());
                     obj.AgentName = selection["AgentName"].ToString();
                     obj.Address = selection["Address"].ToString();
                     obj.City = selection["City"].ToString();
                     obj.Linkman = selection["Linkman"].ToString();
                     obj.Nearby = selection["Nearby"].ToString();
                     obj.Telphone = selection["Telphone"].ToString();
                     obj.LoginName = selection["LoginName"].ToString();
                     obj.WorkHours = selection["WorkHours"].ToString();
                     obj.WorkQQ = selection["WorkQQ"].ToString();
                }
            }
            return obj;
        }

        /// <summary>
        /// Get collection of the Agent
        /// </summary>
        /// <param name="city">city key</param>
        /// <returns>Collection Object</returns>
        public List<Entities.TT_Agent> GetListByCity(string city)
        {
            List<Entities.TT_Agent> list = new List<Entities.TT_Agent>();
            using (SqlStoredProcedure selection = new SqlStoredProcedure("Pro_Agent_Pan_SelectByCity"))
            {
                selection.AddParameter("@City", city);
                while (selection.Read())
                {
                    Entities.TT_Agent item = new Easydp.Models.Entities.TT_Agent();
                    item.AgentID = Convert.ToInt32(selection["AgentID"].ToString());
                    item.LoginName = selection["LoginName"].ToString();
                    item.AgentName = selection["AgentName"].ToString();
                    item.Linkman = selection["Linkman"].ToString();
                    item.Address = selection["Address"].ToString();
                    item.Nearby = selection["Nearby"].ToString();
                    item.City = selection["City"].ToString();
                    item.Telphone = selection["Telphone"].ToString();
                    item.WorkHours = selection["WorkHours"].ToString();
                    item.WorkQQ = selection["WorkQQ"].ToString();
                    item.OrderAcount = selection["orderAcount"].ToString();
                    item.WorkWangWang = selection["WorkWangWang"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public DataSet GetListByArea(string area)
        {
            using (SqlStoredProcedure selection = new SqlStoredProcedure("Pro_Agent_Pan_SelectByArea"))
            {
                selection.AddParameter("@Area", area);
                return selection.GetDataset();
            }
        }

        public List<string> GetAllCity()
        {
            List<string> list = new List<string>();
            using (SqlText selection = new SqlText("SELECT DISTINCT City FROM TT_Agent WHERE allowed =1"))
            {
                while(selection.Read())
                {
                    string city = selection["City"].ToString();
                    list.Add(city);
                }
            }
            return list;
        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// 
        public int Update(Entities.TT_Agent obj)
        {
            using (SqlText updation = new SqlText(@"Update TT_Agent SET 
                                            Address=@Address,
                                            Nearby = @Nearby,
                                            Telphone = @TelPhone,
                                            LinkMan = @LinkMan,
                                            WorkHours = @WorkHours
                                            where AgentID = @AgentID"))
            {
                updation.AddParameter("@Address", obj.Address);
                updation.AddParameter("@Nearby", obj.Nearby);
                updation.AddParameter("@TelPhone", obj.Telphone);
                updation.AddParameter("LinkMan", obj.Linkman);
                updation.AddParameter("@AgentID", obj.AgentID);
                updation.AddParameter("@WorkHours", obj.WorkHours);
                return updation.ExecuteNonQuery();
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetDataset(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select AgentID,LoginName,Password,AgentName,Linkman,Address,Nearby,City,Telphone,CreateOn,WorkHours,WorkQQ,Allowed ");
            strSql.Append(" FROM TT_Agent ");
            if (!string.IsNullOrEmpty(strWhere))
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }


        /// <summary>
        /// 得到list列表
        /// </summary>
        /// 
        public List<Entities.TT_Agent> GetList(int AgentID)
        {
            List<Entities.TT_Agent> list = new List<Easydp.Models.Entities.TT_Agent>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select AgentID,LoginName,Password,AgentName,Linkman,Address,Nearby,City,Telphone,CreateOn,WorkHours,WorkQQ,WorkWangWang ");
            strSql.Append(" FROM TT_Agent ");
            strSql.Append(" where AgentID=@AgentID ");
            using (SqlText selectlist = new SqlText(strSql.ToString()))
            {
                selectlist.AddParameter("@AgentID", AgentID);
                while (selectlist.Read())
                {
                    Entities.TT_Agent model = new Easydp.Models.Entities.TT_Agent();
                    if (selectlist["AgentID"].ToString() != "")
                    {
                        model.AgentID = int.Parse(selectlist["AgentID"].ToString());
                    }
                    model.LoginName = selectlist["LoginName"].ToString();
                    model.Password = selectlist["Password"].ToString();
                    model.AgentName = selectlist["AgentName"].ToString();
                    model.Linkman = selectlist["Linkman"].ToString();
                    model.Address = selectlist["Address"].ToString();
                    model.Nearby = selectlist["Nearby"].ToString();
                    model.City = selectlist["City"].ToString();
                    model.Telphone = selectlist["Telphone"].ToString();
                    model.WorkQQ = selectlist["WorkQQ"].ToString();
                    model.WorkWangWang = selectlist["WorkWangWang"].ToString();
                    if (selectlist["CreateOn"].ToString() != "")
                    {
                        model.CreateOn = DateTime.Parse(selectlist["CreateOn"].ToString());
                    }
                    model.WorkHours = selectlist["WorkHours"].ToString();
                    list.Add(model);
                }
            }
            return list;
        }

        public int Insert(Entities.TT_Agent obj)
        {
            using (SqlStoredProcedure insertion = new SqlStoredProcedure("TT_Agent_Insert"))
            {
                insertion.AddParameter("@LoginName",obj.LoginName);
                insertion.AddParameter("@AgentName", obj.AgentName);
                insertion.AddParameter("@LinkMan", obj.Linkman);
                insertion.AddParameter("@Address", obj.Address);
                insertion.AddParameter("@City", obj.City);
                insertion.AddParameter("@TelPhone", obj.Telphone);
                insertion.AddParameter("@QQ", obj.WorkQQ);
                insertion.AddParameter("@WorkWangWang", obj.WorkWangWang);
                insertion.AddParameter("@WorkHours", obj.WorkHours);
                insertion.AddParameter("@AlipayAccount", obj.AlipayAccount);
                return insertion.ExecuteNonQuery();
            }
        }
    }
}
